<template>
  <el-dialog
    class="title"
    :title="titleName"
    top="166px"
    :visible.sync="DialogVisible"
    width="306px"
  >
    <el-form
      size="small"
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
    >
      <el-form-item prop="Acount">
        <el-input
          prefix-icon="el-icon-user-solid"
          class="acount-input"
          type="Acount"
          v-model="ruleForm.Acount"
          autocomplete="off"
          style="width: 100%"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>

      <el-form-item prop="pass">
        <el-input
          prefix-icon="el-icon-view"
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item v-if="isRegister" prop="checkPass">
        <el-input
          prefix-icon="el-icon-view"
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          placeholder="请再次输入密码"
        ></el-input>
      </el-form-item>

      <el-button type="primary" @click="submitForm('ruleForm')">{{
        titleName
      }}</el-button>
    </el-form>
  </el-dialog>
</template>

<script>
import { setRegister, login } from "../api/login";
import { setToken } from "../utils/cookie";
export default {
  name: "RegisterDialog",
  data() {
    const regUsername = /^[a-zA-Z]{1}\w{4,15}$/; // 字母开头，长度5-16之间，允许字母数字下划线
    const regPassword = /^[a-zA-Z]{1}\w{5,17}$/; // 字母开头，长度6-18之间，允许字母数字下划线
    var validateAccount = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (!regUsername.test(value)) {
        callback(new Error("字母开头，长度5-16之间，允许字母数字下划线"));
      } else {
        if (this.ruleForm.checkAcount !== "") {
          this.$refs.ruleForm.validateField("checkAcount");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!regPassword.test(value)) {
        callback(new Error("字母开头，长度6-16之间，允许字母数字下划线"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (!regUsername.test(value)) {
        callback(new Error("字母开头，长度5-16之间，允许字母数字下划线"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      DialogVisible: false,
      titleName: "",
      isRegister: {
        type: Boolean,
      },

      ruleForm: {
        Acount: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        Acount: [{ validator: validateAccount, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },

  mounted() {},

  methods: {
    showRegister() {
      this.DialogVisible = true;
      this.titleName = "注册";
      this.isRegister = true;
      this.ruleForm.Acount = "";
      this.ruleForm.pass = "";
      this.ruleForm.checkPass = "";
    },

    showLogin() {
      this.DialogVisible = true;
      this.titleName = "登录";
      this.isRegister = false;
      this.ruleForm.Acount = "";
      this.ruleForm.pass = "";
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isRegister) {
            setRegister({
              username: this.ruleForm.Acount,
              password: this.ruleForm.pass,
            }).then(() => {
              this.DialogVisible = false;
            });
          } else {
            login({
              username: this.ruleForm.Acount,
              password: this.ruleForm.pass,
            }).then((res) => {
              setToken("token", res.data);
              this.DialogVisible = false;
              this.$store.dispatch("getUsername");
              this.$store.commit("setLoginState");
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  text-align: center;
}
.el-form-item {
  margin-bottom: 24px;
}
.el-button {
  margin: 8px 0;
  width: 100%;
}
</style>
